"""
@Author  : 吕申凯
@Time    : 2022/9/19 21:51
@File    : 统计直方图和折线图.py
@Function: 统计直方图的实例
"""

import numpy as np
import matplotlib.pyplot as plt
from torch.utils.tensorboard import SummaryWriter
from tools.common_tools import set_seed


def histogram_test():
    """
        做直方图统计
    :return:
    """
    # 构建SummaryWriter
    writer = SummaryWriter(comment='test_comment', filename_suffix="test_suffix")

    for x in range(2):
        np.random.seed(x)

        # 0-100的整数的100个数据
        data_union = np.arange(100)
        # 随机采样得到的1000个数据
        data_normal = np.random.normal(size=1000)

        # 添加到对应的文件中
        writer.add_histogram('distribution union', values=data_union, global_step=x)
        writer.add_histogram('distribution normal', data_normal, x)

        # 用python画图
        # plt.subplot(121).hist(data_union, label="union")
        # plt.subplot(122).hist(data_normal, label="normal")
        # plt.legend()
        # plt.show()

    writer.close()


if __name__ == "__main__":
    set_seed(1)  # 设置随机种子

    histogram_test()
